#![allow(non_snake_case)]
use std::time::Duration;

use crate::context_menu::init_context_menu;
use crate::layout::content::Content;
use crate::layout::login::Login;
use crate::layout::menu_state::MenuState;
use leptos::prelude::*;
use leptos_meta::*;
use leptos_router::components::{FlatRoutes, Route, Router, RoutingProgress};
use leptos_router::path;
use shq_common::prelude::User;

pub fn shell(options: LeptosOptions) -> impl IntoView {
    view! {
        <!DOCTYPE html>
        <html lang="en">
            <head>
                <meta charset="utf-8"/>
                <meta name="viewport" content="width=device-width, initial-scale=1"/>
                <AutoReload options=options.clone() />
                <HydrationScripts options/>
                <link rel="stylesheet" id="leptos" href="/pkg/shq_pas_ui.css"/>
                <script src="/echarts.min.js"></script>
                <MetaTags/>
            </head>
            <body>
                <App/>
            </body>
        </html>
    }
}

#[component]
pub fn App() -> impl IntoView {
    provide_meta_context();
    let menu_items = RwSignal::new(MenuState::new());
    let (content_menus,_) = signal(init_context_menu());
    let curr_user: RwSignal<Option<User>> = RwSignal::new(None);
    provide_context(menu_items);
    provide_context(curr_user);
    provide_context(content_menus);
    let (is_routing, set_is_routing) = signal(false);
    view! {
        <Router set_is_routing>
            <div class="routing-progress">
                <RoutingProgress is_routing max_time=Duration::from_millis(250)/>
            </div>
            <main>
                <FlatRoutes fallback=|| "Not found.">
                    <Route path=path!("") view=Content/>
                    <Route path=path!("/login") view=Login/>
                </FlatRoutes>
            </main>
        </Router>
    }
}
